package com.study.demo_resttemplate.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.demo_resttemplate.entity.sdkentity.*;
import com.study.demo_resttemplate.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Name: RestTemplateTest
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/15 11:38
 * @Remark: 特殊说明
 * @Version: 1.0
 */
@Component
public class SDKInteface {
    @Autowired
    private RestTemplate restTemplate;

    private String SDK_BASE="http://127.0.0.1:30505/";

    public SDKApiSign generateApiSign(String secretKey,String secretId){
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Accept", MediaType.ALL_VALUE);

        Map<String,Object> request = new HashMap<>();
        request.put("secretKey",secretKey);
        request.put("secretId",secretId);
        HttpEntity httpEntity=new HttpEntity(request,headers);
        JSONObject jsonObject = restTemplate.postForObject(SDK_BASE + "generateApiSign", httpEntity, JSONObject.class);
        return JSON.parseObject(jsonObject.toJSONString(),SDKApiSign.class);
    }

    public HttpEntity getHeader(SDKApiSign sdkApiSign,String secretId){
        HttpHeaders headers=new HttpHeaders();
        headers.add("Secret-Id",secretId);
        headers.add("Signature-Time",sdkApiSign.getSignData().getSignatureTime());
        headers.add("Signature",sdkApiSign.getSignData().getSignature());
        headers.add("Nonce",sdkApiSign.getSignData().getNonce().toString());
        headers.add("Content-Type","application/json;charset=utf-8");
        return new HttpEntity(headers);
    }

    public SDKApiMnemonic createMnemonic(){
        return restTemplate.postForObject(SDK_BASE + "createMnemonic", null, SDKApiMnemonic.class);
    }

    public SDKApiKeyPair deriveKeyPair(){
        SDKApiMnemonic mnemonic = createMnemonic();
        Map<String,Object> request = new HashMap<>();
        request.put("mnemonic",mnemonic.getMnemonic());
        request.put("index",0);
        return restTemplate.postForObject(SDK_BASE + "deriveKeyPair", request, SDKApiKeyPair.class);
    }

    public SDKApiKeyPair priKey2PubKey(String pri) {
        Map<String,Object> request = new HashMap<>();
        request.put("pri",pri);
        return restTemplate.postForObject(SDK_BASE + "priKey2PubKey", request, SDKApiKeyPair.class);
    }

    public SDKApiKeyPair pubKey2Address(String pubKey) {
        Map<String,Object> request = new HashMap<>();
        request.put("pubKey",pubKey);
        return restTemplate.postForObject(SDK_BASE + "pubKey2Address", request, SDKApiKeyPair.class);
    }

    public SDKApiKeyPair priKey2Address(String priKey) {
        Map<String,Object> request = new HashMap<>();
        request.put("priKey",priKey);
        return restTemplate.postForObject(SDK_BASE + "priKey2Address", request, SDKApiKeyPair.class);
    }

    public SDKApiSignData signByPriKey(String priKey, String data) {
        Map<String,Object> request = new HashMap<>();
        request.put("PriKey",priKey);
        request.put("Data",data);
        return restTemplate.postForObject(SDK_BASE + "signByPriKey", request, SDKApiSignData.class);
    }

    public SDKApiSignData verifyByPubKey(String pubKey, String signedData, String data) {
        Map<String,Object> request = new HashMap<>();
        request.put("pubKey",pubKey);
        request.put("signedData",signedData);
        request.put("data",data);
        return restTemplate.postForObject(SDK_BASE + "verifyByPubKey", request, SDKApiSignData.class);
    }

    public SDKApi uploadToCos() {
        Map<String,Object> request = new HashMap<>();
        request.put("cosPath", "desktop");
        request.put("tempSecretId","");
        request.put("tempSecretKey","");
        request.put("sessionToken", "123");
        request.put("filePath", FileUtil.encryptToBase64("C:\\Users\\yn\\Desktop\\keystore生成.txt"));
        return restTemplate.postForObject(SDK_BASE + "uploadToCos", request, SDKApi.class);
    }

    public SDKApiSM3Hash sm3Hash() {
        Map<String,Object> request = new HashMap<>();
        request.put("data", FileUtil.encryptToBase64("C:\\Users\\yn\\Desktop\\keystore生成.txt"));
        return restTemplate.postForObject(SDK_BASE + "sm3Hash", request, SDKApiSM3Hash.class);
    }
}
